-- 跨服精英赛 帮助函数
local CrossServerEliteMatchHelper = {}
local WeekStr = {"一", "二", "三", "四", "五", "六", "七"}

-- 获取Tips
function CrossServerEliteMatchHelper:getTipsStr(data)
    local tipsStr = ""
    local isOpening = UD.csEliteMatch:isOpenByPlayId(data.playId) -- 是否在开放时间段内
    if data.endTime < app:getServerTime() then -- 当前赛季结束新赛季还没开始
        tipsStr = L("EliteMatchTip47")
    elseif data.isOpen then
        if isOpening then
            if UD.csEliteMatch:getState() == "matching" then
                tipsStr = L("EliteMatchTip3")
            else
                tipsStr = L("EliteMatchTip2")
            end
        else
            if UD.csEliteMatch:getEndTime() < app:getServerTime() then
                tipsStr = L("EliteMatchTip52")
            else
                tipsStr = string.format(L("EliteMatchTip4"), data.conf.dailyTime)
            end
        end
    else
        local week = string.split(data.conf.weekTime, ";")
        local dayStr
        for i, v in ipairs(week) do
            if dayStr == nil then
                dayStr = WeekStr[tonumber(v)]
            else
                dayStr = dayStr.."、"..WeekStr[tonumber(v)]
            end
        end
        if dayStr == nil then
            tipsStr = L("EliteMatchTip14")
        else
            tipsStr = string.format(L("EliteMatchTip5"), dayStr)
        end
    end
    return tipsStr
end

-- 根据玩法获得奖励和惩罚
function CrossServerEliteMatchHelper:getLootRewardByPlayId(id, level, flag)
    local rewards = {} -- 胜利奖励
    local punishs -- 失败惩罚
    local expId = 151 -- 经验ID
    if id == Enums.TeamTarget.eliteMatchArena then
        expId = 151
    elseif id == Enums.TeamTarget.eliteMatchDuel then
        expId = 152
    end
    local playInfo = UD.csEliteMatch:getPlayInfoById(id)
    local conf = GD:getEliteMatchById(id)
    local expConf = GD:getEliteMatchExp(level, id)
    if not conf or not expConf then return end
    if conf.victoryReward then -- 胜利奖励
        -- 不判断金币到达上限
        local params = {}
        params.data = conf.victoryReward
        params.textStr = L("EliteMatchTip26")
        params.textColor = cc.c3b(156, 255, 0)
        if playInfo and playInfo.todayReward >= playInfo.todayRewardLimit then
            params.textStr = L("EliteMatchTip28")
            params.textColor = cc.c3b(255, 48, 0)
        end
        if flag then
            params.isReward = true
        end
        table.insert(rewards, params)
    end
    if expConf and expConf.winExp and tonumber(expConf.winExp) > 0 then
        -- 胜利经验不等于0才显示
        local info = table.concat({expId, 0, tonumber(expConf.winExp)}, "|")
        local params = {}
        params.data = info
        params.textStr = L("EliteMatchTip26")
        params.textColor = cc.c3b(156, 255, 0)
        if flag then
            params.isReward = true
        end
        table.insert(rewards, params)
    end
    if conf.participationReward then -- 参与奖励
        -- 金币达到上限不显示
        if playInfo and playInfo.todayReward < playInfo.todayRewardLimit then
            local params = {}
            params.data = conf.participationReward
            params.textStr = L("EliteMatchTip54")
            params.textColor = cc.c3b(21, 188, 255)
            if flag then
                params.isReward = true
            end
            table.insert(rewards, params)
        end  
    end
    if expConf and expConf.loseExp and tonumber(expConf.loseExp) > 0 then
        -- 失败经验不等于0才显示
        local info = table.concat({expId, 0, tonumber(expConf.loseExp)}, "|")
        local params = {}
        params.data = info
        params.textStr = L("EliteMatchTip27")
        params.textColor = cc.c3b(255, 48, 0)
        if flag then
            params.isReward = false
        end
        if flag then
            table.insert(rewards, params)
        else
            punishs = params
        end
    end
    return rewards, punishs
end

-- 根据玩法获得赛季奖励(达到传奇等级如果进入排行榜前100名显示排行榜奖励)
function CrossServerEliteMatchHelper:getSeasonReward(id, level)
    local rewards = {}
    local conf, dropIds
    local expConf = GD:getEliteMatchExp(level, id)
    if level == 0 then
        local rank = UD.csEliteMatch:getMyRankByPlayId(id)
        conf = GD:getEliteMatchRewardById(id, rank)
        if conf then
            dropIds = string.split(conf.reward, ";")
        else
            conf = expConf
            dropIds = conf and string.split(expConf.seasonReward, ";")
        end
    else
        conf = expConf
        dropIds = conf and string.split(expConf.seasonReward, ";")
    end  
    for _, dropId in ipairs(dropIds) do
        local rets = UD:parseDropIDToBagItem(dropId)
        if rets then
            for _, itemData in ipairs(rets) do
                table.insert(rewards, itemData)
            end
        end
    end
    return rewards
end

-- 根据玩法获得当天开启结束时间
function CrossServerEliteMatchHelper:getCurOpenTimeById(id)
    local curOpenTime = {} -- 当天开放时间
    local conf = GD:getEliteMatchById(id)
    if conf then
        local isOpenToday = UD.csEliteMatch:isTodayOpenByPlayId(id)
        if isOpenToday then
            curOpenTime.starttime = UD.csEliteMatch:getStartTime()
            curOpenTime.endtime = UD.csEliteMatch:getEndTime()
        end
    end
    return curOpenTime
end

-- 根据玩法获得下一次开启时间
function CrossServerEliteMatchHelper:getNextOpenTimeById(id)
    local nextOpenTime -- 下一次开放天数
    local conf = GD:getEliteMatchById(id)
    if not conf then return end
    local serverDate = app.session:getServerDate()
    local wday = TimeUtil.getWeekByYearMonthDay(serverDate.year, serverDate.month, serverDate.day)
    local timeArray = string.split(conf.dailyTime,"-")
    local time1 = string.split(timeArray[1],":")
    local time2 = string.split(timeArray[2],":")
    local weekAry = string.split(conf.weekTime, ";")
    local diffDay = 0
    for i, v in ipairs(weekAry) do
        if wday < tonumber(v) then
            diffDay = tonumber(v) - wday
            break
        end
    end
    if diffDay == 0 then
        -- 跨周了
        diffDay = tonumber(weekAry[1])
    end
    serverDate.hour = tonumber(time1[1])
    serverDate.min = tonumber(time1[2])
    serverDate.sec = 0
    nextOpenTime = os.time(serverDate)
    nextOpenTime = nextOpenTime + diffDay*24*3600
    return nextOpenTime
end

function CrossServerEliteMatchHelper:getExtraTipsContent(data, id)
    local normalColor = cc.c3b(196, 169, 100)
    local redColor = cc.c3b(255, 0, 0)
    local contents = {}
    table.insert(contents, {text = data:getDesc(), color = normalColor})
    local playInfo = UD.csEliteMatch:getPlayInfoById(id)
    if playInfo then
        local str = string.format(L("EliteMatchTip44"), playInfo.todayReward, playInfo.todayRewardLimit)
        local strColor = normalColor
        if playInfo.todayReward >= playInfo.todayRewardLimit then
            strColor = redColor
        end
        table.insert(contents, {text = str, color = strColor})
    end
    return contents
end

function CrossServerEliteMatchHelper:getNameByPlayId(id)
    local conf = GD:getEliteMatchById(id)
    if conf then
        return L(conf.name)
    end
end

return CrossServerEliteMatchHelper